package com.gfhd.multithread.thread_safe;

import lombok.Data;

import java.util.concurrent.locks.ReentrantLock;

@Data
public class Account {
    private String cardId;
    private double money;
    private final ReentrantLock lock = new ReentrantLock();

    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }

    public Account() {
    }

    public static void run(){
        synchronized (Account.class){

        }
    }


    public synchronized void drawMoney(double money){
        String name = Thread.currentThread().getName();
        lock.lock();
        try{


        synchronized (this) {
            if(this.money>=money){
                System.out.println(name+"来取钱，吐出"+money);
                this.money -= money;
                System.out.println(name+"取钱后，剩余"+this.money);
            }else {
                System.out.println(name+"来取钱，余额不足！");
            }
        }

        }finally {
            lock.unlock();
        }
    }

}
